/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/


#include "FPSTracker.h"
#include "DebugVariableTracker.h"
#include "..\..\XUtilities\Utilities.h"

namespace XF
{
	FPSTracker::FPSTracker(Application *ownerApp)
		: SystemComponent(ownerApp)
	{
	}

	FPSTracker::~FPSTracker()
	{
	}

	bool FPSTracker::OnInitialise()
	{
		for(int i = 0; i < XF_FPS_TRACKER_ACCURACY; ++i)
			mFPSValues[i] = 0.0f;

		DebugVariableTracker::RegisterVariable("float", "FPS");
		return true;
	}

	void FPSTracker::OnUpdate( double dT )
	{
		for(int i = 0; i < XF_FPS_TRACKER_ACCURACY - 1; ++i)
			mFPSValues[i] = mFPSValues[i + 1];

		mFPSValues[XF_FPS_TRACKER_ACCURACY - 1] = 1.0f / dT;

		float fps = 0.0f;

		for(int i = 0; i < XF_FPS_TRACKER_ACCURACY; ++i)
			fps += mFPSValues[i];

		fps /= XF_FPS_TRACKER_ACCURACY;

		DebugVariableTracker::UpdateVariable("FPS", Utilities::ToString(fps));
	}

	void FPSTracker::OnShutDown()
	{
		DebugVariableTracker::UnregisterVariable("FPS");
	}
}
